Completed
Push — master ( 182512...5dcd2b )
by greg
02:20
created

revision.js ➔ removeStatusAndDateFromFileName   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
dl 0
loc 3
rs 10
nop 1
1
import path from 'path'
2
3
import {
4
  config,
5
  cmsData,
6
  coreUtils,
7
  Manager
8
} from '../../'
9
10
export function getFilesRevision(urls, fileName) {
11
  var res = []
12
  var number = 1
13
  var tplUrl = cmsData.file.fromUrl(fileName)
14
  fileName = fileName.split('/')
15
  fileName = fileName[fileName.length - 1]
16
  var publishDate = new Date()
17
  var json = null
18
19
  if(coreUtils.file.exist(tplUrl.publish.json)) {
20
    json = cmsData.file.get(tplUrl.publish.json)
21
    if(json != null && json[config.meta.name] != null) {
22
      publishDate = new Date(json[config.meta.name].latest.date)
23
    }
24
  }
25
26
  var publishVersion = false
27
  urls.forEach(function (urlObj) {
28
    var fileData = cmsData.fileAttr.get(urlObj.cleanPath)
29
    if(fileData.s === 'd' && cmsData.fileAttr.delete(urlObj.cleanPath) == cmsData.fileAttr.delete(fileName)) {
30
      var currentDate = new Date(urlObj.date)
31
      if(currentDate.getTime() > publishDate.getTime()) {
32
        if(!publishVersion && res[res.length - 1] != null) {
33
          res[res.length - 1].publishedDate = 'same'
34
        }
35
        publishVersion = true
36
        urlObj.publishedDate = 'after'
37
        
38
      }else if(currentDate.getTime() === publishDate.getTime()) {
39
        urlObj.publishedDate = 'same'
40
        publishVersion = true
41
      }else {
42
        urlObj.publishedDate = 'before'
43
      }
44
      urlObj.version = number
45
      number = number + 1
46
47
      var tplUrlObj = cmsData.file.fromUrl(urlObj.path)
48
      if(coreUtils.file.exist(tplUrlObj.publish.json)) {
49
        var jsonObj = cmsData.file.get(tplUrlObj.publish.json)
50
        urlObj[config.meta.name] = jsonObj[config.meta.name]
51
      }
52
      res.push(urlObj)
53
    }
54
  })
55
  return res
56
}
57
58
/**
59
 * Create and array of doc file path containing the versions of this doc
60
 * @param  {String} path of a doc
0 ignored issues
show
Documentation introduced by
The parameter path does not exist. Did you maybe forget to remove this comment?
Loading history...
61
 * @return {Array} the versions of the doc
62
 */
63
export function getVersions(docPath) {
64
  var result = []
65
  var files = Manager.instance.getList()
66
  var dataFile = docPath.replace('.' + config.files.templates.extension, '.json')
67
68
  Array.prototype.forEach.call(files, (file) => {
69
    if (file.path.indexOf(dataFile) > -1) {
70
      result = file.revisions
71
    }
72
  })
73
  return result
74
}
75
76
/**
77
 * Return the revision from document html file path
78
 * if the docPath contains abe revision [status]-[date] will try to return this revision
79
 * else it will return the latest revision
80
 * or null
81
 * 
82
 * @param  {String} html path
0 ignored issues
show
Documentation introduced by
The parameter html does not exist. Did you maybe forget to remove this comment?
Loading history...
83
 * @return {Object} file revision | null
84
 */
85
export function getDocumentRevision(docPath) {
86
  var result = null
87
  var documentPath = docPath
88
  var latest = true
89
90
  if(cmsData.fileAttr.test(documentPath)){
91
    latest = false
92
    documentPath = cmsData.fileAttr.delete(documentPath)
93
  }
94
  var revisions = getVersions(documentPath)
95
  if (latest && revisions.length >= 0) {
96
    result = revisions[0]
97
  } else if (!latest) {
98
    Array.prototype.forEach.call(revisions, (revision) => {
99
      if (revision.html === docPath) {
100
        result = revision
101
      }
102
    })
103
    if (result === null && revisions.length >= 0) {
104
      result = revisions[0]
105
    }
106
  }
107
  return result
108
}
109
110
export function getStatusAndDateToFileName(date) {
111
  var res = date.substring(0, 4) + '-'
112
            + date.substring(4, 6) + '-'
113
            + date.substring(6, 11) + ':'
114
            + date.substring(11, 13) + ':'
115
            + date.substring(13, 15) + '.'
116
            + date.substring(15, 19)
117
  return res
118
}
119
120
export function removeStatusAndDateFromFileName(date) {
121
  return date.replace(/[-:\.]/g, '')
122
}
123
124
export function filePathInfos(pathFolder) {
125
  var pathArr = pathFolder.split('/')
126
  var name = pathArr[pathArr.length - 1]
127
128
  var rootArr = config.root.split('/')
129
  var website = rootArr[pathArr.length - 1]
130
  return {
131
    'name': name,
132
    'path': pathFolder,
133
    'website': website,
134
    'cleanPath': pathFolder.replace(config.root, '').replace(/\/$/, ''),
135
    'type': 'folder'
136
  }
137
}
138
139
export function getFilesMerged(files) {
140
  var merged = {}
141
  var arMerged = []
142
  
143
  Array.prototype.forEach.call(files, (file) => {
144
    var cleanFilePath = file.cleanFilePath
145
146
    var fileStatusIsPublish = cmsData.fileAttr.get(file.cleanPath)
147
    if(fileStatusIsPublish.s != null && file.abe_meta.status === 'publish') {
148
      file.abe_meta.status = 'draft'
149
    }
150
151
    file.html = path.join('/', file.filePath.replace(/\.json/, `.${config.files.templates.extension}`))
152
    if (file.abe_meta.status === 'publish') {
153
      file.htmlPath = path.join(config.root, config.publish.url, path.join('/', file.filePath.replace(/\.json/, `.${config.files.templates.extension}`)))
154
    }else {
155
      file.htmlPath = path.join(config.root, config.draft.url, path.join('/', file.filePath.replace(/\.json/, `.${config.files.templates.extension}`)))
156
    }
157
158
    if(merged[cleanFilePath] == null) {
159
      merged[cleanFilePath] = {
160
        name: cmsData.fileAttr.delete(file.name),
161
        path: cmsData.fileAttr.delete(file.path),
162
        html: cmsData.fileAttr.delete(path.join('/', file.filePath.replace(/\.json/, `.${config.files.templates.extension}`))),
163
        htmlPath: path.join(config.root, config.publish.url, path.join('/', cmsData.fileAttr.delete(file.filePath.replace(/\.json/, `.${config.files.templates.extension}`)))),
164
        cleanPathName: file.cleanPathName,
165
        cleanPath: file.cleanPath,
166
        cleanName: file.cleanName,
167
        cleanNameNoExt: file.cleanNameNoExt,
168
        cleanFilePath: file.cleanFilePath,
169
        filePath: cmsData.fileAttr.delete(file.filePath),
170
        revisions: []
171
      }
172
    }
173
174
    merged[cleanFilePath].revisions.push(JSON.parse(JSON.stringify(file)))
175
  })
176
177
  // return merged
178
  Array.prototype.forEach.call(Object.keys(merged), (key) => {
179
    var revisions = merged[key].revisions
180
    revisions.sort(coreUtils.sort.predicatBy('date', -1))
181
    if(revisions[0] != null) {
182
      merged[key].date = revisions[0].date
183
    }
184
185
    Array.prototype.forEach.call(revisions, (revision) => {
186
      
187
      var status = revision.abe_meta.status
188
189
      if (status === 'publish') {
190
        merged[key][status] = revision
191
      }else {
192
        merged[key][status] = {}
193
      }
194
      merged[key][status].path = revision.path
195
      merged[key][status].html = revision.html
196
      merged[key][status].htmlPath = revision.htmlPath
197
      merged[key][status].date = new Date(revision.date)
198
      merged[key][status].link = revision.abe_meta.link
199
    })
200
201
    merged[key].revisions = revisions
202
203
    merged[key].date = revisions[0].date
204
    merged[key].cleanDate = revisions[0].cleanDate
205
    merged[key].duration = revisions[0].duration
206
    merged[key].abe_meta = revisions[0].abe_meta
207
208
    arMerged.push(merged[key])
209
  })
210
211
  return arMerged
212
}
213
214
export function deleteOlderRevisionByType(fileName, type) {
215
  var folder = fileName.split('/')
216
  var file = folder.pop()
217
  var extension = file.replace(/.*?\./, '')
218
  folder = folder.join('/')
219
  var stat = fse.statSync(folder)
0 ignored issues
show
Bug introduced by
The variable fse seems to be never declared. If this is a global, consider adding a /** global: fse */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
220
  if(stat){
221
    var files = FileParser.getFiles(folder, true, 1, new RegExp('\\.' + extension))
0 ignored issues
show
Bug introduced by
The variable FileParser seems to be never declared. If this is a global, consider adding a /** global: FileParser */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
222
    files.forEach(function (fileItem) {
223
      var fname = cmsData.fileAttr.delete(fileItem.cleanPath)
224
      var ftype = cmsData.fileAttr.get(fileItem.cleanPath).s
225
      if(fname === file && ftype === type){
226
        var fileDraft = fileItem.path.replace(/-abe-./, '-abe-d')
227
        FileParser.removeFile(fileItem.path, FileParser.changePathEnv(fileItem.path, config.data.url).replace(new RegExp('\\.' + extension), '.json'))
0 ignored issues
show
Bug introduced by
The variable FileParser seems to be never declared. If this is a global, consider adding a /** global: FileParser */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
228
        FileParser.removeFile(fileDraft, FileParser.changePathEnv(fileDraft, config.data.url).replace(new RegExp('\\.' + extension), '.json'))
229
      }
230
    })
231
  }
232
}